home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSEBCDIC.C < prev    next >
C/C++ Source or Header  |  1993-05-18  |  6KB  |  171 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bsebcdic.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains ASCII <-> EBCDIC conversion.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <bs.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //    Data    conversion tables
  49. //----------------------------------------------------------------------------
  50. static BYTE a2e[256] =
  51.     {
  52.       0,  1,  2,  3, 55, 45, 46, 47, 22,  5, 37, 11, 12, 13, 14, 15,
  53.      16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
  54.      64, 79,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
  55.     240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
  56.     124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
  57.     215,216,217,226,227,228,229,230,231,232,233, 74,224, 90, 95,109,
  58.     121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
  59.     151,152,153,162,163,164,165,166,167,168,169,192,106,208,161,  7,
  60.      32, 33, 34, 35, 36, 21,  6, 23, 40, 41, 42, 43, 44,  9, 10, 27,
  61.      48, 49, 26, 51, 52, 53, 54,  8, 56, 57, 58, 59,  4, 20, 62,225,
  62.      65, 66, 67, 68, 69, 70, 71, 72, 73, 81, 82, 83, 84, 85, 86, 87,
  63.      88, 89, 98, 99,100,101,102,103,104,105,112,113,114,115,116,117,
  64.     118,119,120,128,138,139,140,141,142,143,144,154,155,156,157,158,
  65.     159,160,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
  66.     184,185,186,187,188,189,190,191,202,203,204,205,206,207,218,219,
  67.     220,221,222,223,234,235,236,237,238,239,250,251,252,253,254,255
  68.     };
  69.  
  70. static BYTE e2a[256] =
  71.     {
  72.       0,  1,  2,  3,156,  9,134,127,151,141,142, 11, 12, 13, 14, 15,
  73.      16, 17, 18, 19,157,133,  8,135, 24, 25,146,143, 28, 29, 30, 31,
  74.     128,129,130,131,132, 10, 23, 27,136,137,138,139,140,  5,  6,  7,
  75.     144,145, 22,147,148,149,150,  4,152,153,154,155, 20, 21,158, 26,
  76.      32,160,161,162,163,164,165,166,167,168, 91, 46, 60, 40, 43, 33,
  77.      38,169,170,171,172,173,174,175,176,177, 93, 36, 42, 41, 59, 94,
  78.      45, 47,178,179,180,181,182,183,184,185,124, 44, 37, 95, 62, 63,
  79.     186,187,188,189,190,191,192,193,194, 96, 58, 35, 64, 39, 61, 34,
  80.     195, 97, 98, 99,100,101,102,103,104,105,196,197,198,199,200,201,
  81.     202,106,107,108,109,110,111,112,113,114,203,204,205,206,207,208,
  82.     209,126,115,116,117,118,119,120,121,122,210,211,212,213,214,215,
  83.     216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,
  84.     123, 65, 66, 67, 68, 69, 70, 71, 72, 73,232,233,234,235,236,237,
  85.    125, 74, 75, 76, 77, 78, 79, 80, 81, 82,238,239,240,241,242,243,
  86.     92,159, 83, 84, 85, 86, 87, 88, 89, 90,244,245,246,247,248,249,
  87.     48, 49, 50, 51, 52, 53, 54, 55, 56, 57,250,251,252,253,254,255
  88.     };
  89.  
  90. //----------------------------------------------------------------------------
  91. //   Description:    Convert string from ASCII to EBCDIC
  92. //    Parameters:    psz     String to convert.
  93. //       Returns:    Pointer to string
  94. //----------------------------------------------------------------------------
  95. PSZ FN_E stra2e(PSZ psz)
  96. {
  97.     PSZ pszSave = psz;
  98.  
  99.     Assert(psz);
  100.     for (; psz[0]; psz++)
  101.         psz[0] = a2e[(BYTE)psz[0]];
  102.  
  103.     return pszSave;
  104. }
  105.  
  106. //----------------------------------------------------------------------------
  107. //   Description:    Convert string from EBCDIC to ASCII
  108. //    Parameters:    psz     String to convert.
  109. //       Returns:    Pointer to string
  110. //----------------------------------------------------------------------------
  111. PSZ FN_E stre2a(PSZ psz)
  112. {
  113.     PSZ pszSave = psz;
  114.  
  115.     Assert(psz);
  116.     for (; psz[0]; psz++)
  117.         psz[0] = e2a[(BYTE)psz[0]];
  118.  
  119.     return pszSave;
  120. }
  121.  
  122.  
  123. //----------------------------------------------------------------------------
  124. //   Description:    Run standard test suite
  125. //    Parameters:    sTest        Test to run.
  126. //                                        0        Run all default tests (except).
  127. //       Returns:    TRUE if successful.
  128. //----------------------------------------------------------------------------
  129. #if COMPILE_TEST
  130. BOOL FN EbcdicTest(SHORT sTest)
  131. {
  132. static CHAR szString[] = "String to convert";
  133.     int i, j, k;
  134.  
  135.     NOTUSED(sTest);
  136.     Output(" Input = [%s]\n", szString);
  137.     stra2e(szString);
  138.     stre2a(szString);
  139.     Output("Output = [%s]\n", szString);
  140.  
  141. #define CHARVAL(ch)     ((isascii((ch)) && isprint((ch))) ? (ch): ' ')
  142.  
  143.     printf("ASCII to EBCDIC:\n\n");
  144.     for (i = k = 0; i < 64; ++i)
  145.         {
  146.         for (j = 0; j < 4; ++j, ++k)
  147.             {
  148.             printf("(%c) %3d = %3d  ",
  149.                 CHARVAL(k),    k, a2e[k]);
  150.             }
  151.         printf("\n");
  152.         }
  153.     printf("EBCDIC to ASCII:\n\n");
  154.     for (i = k = 0; i < 64; ++i)
  155.         {
  156.         for (j = 0; j < 4; ++j, ++k)
  157.             {
  158.             printf("(%c) %3d = %3d  ",
  159.                 CHARVAL(e2a[k]),    k, e2a[k]);
  160.             }
  161.         printf("\n");
  162.         }
  163.     return TRUE;
  164. }
  165. #endif
  166. //----------------------------------------------------------------------------
  167. //------------------------------- End of File --------------------------------
  168. //----------------------------------------------------------------------------
  169.  
  170.  
  171.